package cn.net.greenet.send.policy;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.codec.binary.Base64;

/**
 * 将指定的策略发送到策略发送队列Table中
 *
 * @author liangdelin
 */
@WebServlet(name = "HandSendPolicyQueue", urlPatterns = {"/policy/hand_send_policy_queue"})
public class HandSendPolicyQueue extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        List<Map<String, String>> jsonData = new ArrayList<>();  //存放策略库信息
        String json = request.getParameter("json");
        System.out.println("receive json" + json);
        JSONArray jsonArr = JSONArray.fromObject(json);
        for (int i = 0; i < jsonArr.size(); i++) {
            Map<String, String> temporaryData = new HashMap<>();
            JSONObject jo = jsonArr.getJSONObject(i);
            temporaryData.put("type", jo.getString("type"));
            temporaryData.put("instance", new String(Base64.decodeBase64(jo.getString("instance")), "utf-8"));
            temporaryData.put("messageNo", jo.getString("messageNo"));
            temporaryData.put("policyRepositoryID", jo.getString("policyRepositoryID"));
            temporaryData.put("deviceID", jo.getString("deviceID"));
            jsonData.add(temporaryData);
        }
        try (PrintWriter out = response.getWriter()) {
            boolean flag = SendPolicyUntil.writeMessageQueue(jsonData);
            if (flag) {
                out.println("{\"result\":\"true\"}");
            } else {
                out.println("{\"result\":\"false\"}");
            }
            out.flush();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
